<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\PointProduct;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class PointProductController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PointProduct(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('subtitle');
            $grid->column('main_image')->image('', 50, 50);
            $grid->column('carousel_image')->image('', 50, 50);
            $grid->column('point_price');
            $grid->column('bean_price');
            $grid->column('money', '余额');
            $grid->column('exchange_coupon', '兑换券');
            $grid->column('stock');
            $grid->column('sold_num');
            $grid->column('description');
            $grid->column('sku', '规格')->display('规格查看')->modal(function ($modal) {
                $modal->title('规格');
                if ($this->sku) {
                    $sku = json_decode($this->sku, 1);
                    return admin_view('array', ['data' => $sku['sku']]);
                }
            });
            $grid->column('detail');
            $grid->column('status')->switch();
            $grid->column('sort')->editable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('name');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PointProduct(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('subtitle');
            $show->field('main_image');
            $show->field('carousel_image');
            $show->field('point_price');
            $show->field('bean_price');
            $show->field('stock');
            $show->field('sold_num');
            $show->field('description');
            $show->field('sku');
            $show->field('detail');
            $show->field('status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PointProduct(), function (Form $form) {
            $skuParams = [
                // 扩展第一列
                [
                    'name' => '会员价', // table 第一行 title
                    'field' => 'member_price', // input 的 field_name 名称
                    'default' => 0, // 默认值
                ],
                [
                    'name' => 'KC通证', // table 第一行 title
                    'field' => 'bean', // input 的 field_name 名称
                    'default' => 0, // 默认值
                ],
                [
                    'name' => '积分', // table 第一行 title
                    'field' => 'point', // input 的 field_name 名称
                    'default' => 0, // 默认值
                ],
                [
                    'name' => '兑换券',
                    'field' => 'exchange_coupon',
                    'default' => 0,
                ],
                // 扩展第二列
            ];

            $form->display('id');
            $form->text('name');
            $form->text('subtitle');
            $form->image('main_image')->autoUpload();
            $form->multipleImage('carousel_image')->autoUpload();
            $form->number('point_price');
            $form->number('bean_price');
            $form->number('money', '余额');
            $form->number('exchange_coupon', '兑换券');
            $form->number('stock');
            $form->number('sold_num');
            $form->textarea('description');
            $form->sku('sku', '生成规格')->addColumn($skuParams)->required();

            $form->editor('detail');
            $form->switch('status', '上下架')->default(1);
            $form->number('sort');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
